package com.jboa.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.jboa.model.Function;
import com.jboa.model.Module;
import com.jboa.service.FunctionService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class FunctionAction extends ActionSupport{

	
	private Function function;
	private Module module = new Module();
	private List<Function> functionList;
	private FunctionService functionService;
	
	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public Function getFunction() {
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

	public List<Function> getFunctionList() {
		return functionList;
	}

	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}
	
	// ����findFByMId����ķ���
	public String findFByMid() {
		HttpSession hs = ServletActionContext.getRequest().getSession();
		// �ж�m�Ƿ�Ϊ��
		if (module == null || module.getId() == null) {
			// ���mΪ�ս�session�е�midֵ����m��idֵ
			module.setId((Integer) hs.getAttribute("mid"));

		} else {
			// ��ģ��id����session
			hs.setAttribute("mid", module.getId());
		}
		functionList = functionService.findFByMId(module);
		ServletActionContext.getRequest().setAttribute("FNo", functionList.size());
		return SUCCESS;
	}
	
	// ����newFunction����ķ���
	public String newFunction() {
		// ��ģ��id��session��ȡ��
		HttpSession hs = ServletActionContext.getRequest().getSession();
		functionService.newFunction(function, (Integer) hs.getAttribute("mid"));
		return Action.SUCCESS;
	}
	// ����removeFunction����ķ���
	public String removeFunction() {
		function =functionService.findSingleFunction(function);
		functionService.removeFunction(function);
		return SUCCESS;
	}
	// ����findSingleFunction����ķ���
	public String findSingleFunction() {
		function = functionService.findSingleFunction(function);
		return SUCCESS;
	}

	// ����modifyFunction����ķ���
	public String modifyFunction() {
		functionService.modifyFunction(function);
		return SUCCESS;
	}
}
